package com.jielin.handling.util.pdfutil;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;

public class Watermark extends PdfPageEventHelper {
    private static Font FONT;// 设置字体大小

    static {
        BaseFont bfChinese;
        try {
            bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
            FONT = new Font(bfChinese, 18, Font.NORMAL, new BaseColor(BaseColor.LIGHT_GRAY.getRed()
                    , BaseColor.LIGHT_GRAY.getGreen(), BaseColor.LIGHT_GRAY.getBlue(), 150));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //	Font FONT = new Font(FontFamily.ZAPFDINGBATS, 15, Font.BOLD, new GrayColor(0.95f));
    private String waterCont;// 水印内容

    public Watermark() {

    }

    public Watermark(String waterCont) {
        this.waterCont = waterCont;
    }

    public static Font getWatermarkFont() {
        return FONT;
    }

    @Override
    public void onEndPage(PdfWriter writer, Document document) {
		/*for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 5; j++) {
				ColumnText.showTextAligned(writer.getDirectContentUnder(), Element.ALIGN_CENTER,
						new Phrase(this.waterCont == null ? "杰霖科技" : this.waterCont, FONT), (55.5f + i * 125),
						(40.0f + j * 180), writer.getPageNumber() % 2 == 1 ? 45 : -45);
			}
		}*/

        if (this.waterCont == null) {
            System.out.println("无水印内容。");
            return;
        }
        for (int j = 0; j < 3; j++) {
            ColumnText.showTextAligned(writer.getDirectContentUnder(), Element.ALIGN_CENTER,
                    new Phrase(this.waterCont, FONT), (135.5f + j * 170),
                    (130.0f + j * 280), 45);
        }
    }
}
